#include "word_game.h"
#include "ui_word_game.h"
#include <QPainter>
#include <QDebug>
#include <QKeyEvent>

word_game::word_game(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::word_game)
{
    ui->setupUi(this);
    //listLetter.length()
    //resize(600, 480);

    for(int i = 0; i < 10; i++)
    {
        QColor c = QColor(qrand() % 256, qrand() % 256, qrand() % 256);
        //Letter l;//先要创建Letter的对象，但是直接创建临时变量是在栈中
        Letter *l = new Letter();//创建对象空间在堆中
        QPixmap pixmap = QPixmap("://res/images/apple_red.png").scaledToHeight(50);
        //Letter *l = &letters[i];//用数组操作(letters[i].setPos(int x, int y))

        l->setPos(qrand() % width(), qrand() % height() - height());
        l->setValue('a' + qrand() % 26);
        l->setPixmap(pixmap);
        l->setColor(c);
        listLetter << l;//将l追加到容器中
        //listLetter.append(l);

#if 0
        a[i] = 'a' + qrand() % 26;
        x[i] = qrand() % width();
        y[i] = qrand() % height() - height();
#endif
    }

    isHit = false;

    score = 0;
    timerId = startTimer(300);
}

word_game::~word_game()
{
    delete ui;
}

void word_game:: timerEvent(QTimerEvent *e)
{
    Q_UNUSED(e)
    //qDebug("timerEvent");

    for(int i = 0; i < 10; i++)
    {
#if 0
        y[i] ++;
        if(y[i] > height())
        {
            a[i] = 'a' + qrand() % 26;
            x[i] = qrand() % width();
            y[i] = 0;
            score -= 10;
            ui->lcdNumber_score->display(score);
        }
#endif
        Letter *l = listLetter[i];//Letter *l = listLetter.at(i);//可以用于容器，但不能用于数组
        //listLetter[i]->setPosY(listLetter[i]->getPosY() + 10);
        l->setPosY(l->getPosY() + 10);//先取出y的值加10，然后重新设置y的值
        //l->setPosY(l->getPosY() + l->getSpeed());//考虑速度
        if(l->getPosY() > height())
        {
            l->setValue('a' + qrand() % 26);
            l->setPos(qrand() % width(), 0);
            QColor c = QColor(qrand() % 256, qrand() % 256, qrand() % 256);
            l->setColor(c);
            score -= 10;
            ui->lcdNumber_score->display(score);
        }
    }
    update();
}

void word_game:: keyPressEvent(QKeyEvent *e)
{
    qDebug() << "key: " << e->key() << e->text();
    for(int i = 0; i < 10; i++)
    {
#if 0
        if(a[i] == e->key() + ('a' - 'A'))
        {
            a[i] = 'a' + qrand() % 26;
            x[i] = qrand() % width();
            y[i] = 0;//当字母相同时，增加y的值回零，实现字母消除
            score += 10;
            ui->lcdNumber_score->display(score);
        }
#endif

        Letter *l = listLetter[i];
        if(l->getValue() == e->key() + ('a' - 'A'))
        {
            l->setValue('a' + qrand() % 26);
            l->setPosX(qrand() % width());
            l->setPosY(0);
            QColor c = QColor(qrand() % 256, qrand() % 256, qrand() % 256);
            l->setColor(c);
            score += 10;
            ui->lcdNumber_score->display(score);
        }
    }
    update();//按键操作实时刷新，反应更快，没有100ms的延迟
}

void word_game:: paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e)
    //qDebug("paintEvent");
    QPainter p(this);

    QFont font;//字体设置
    font.setPointSize(20);

    p.drawPixmap(0, 0, QPixmap("://res/images/apple_red.png").scaledToHeight(50));

//    p.setBrush(Qt:: black);
//    p.drawRect(0, 0, width(), height());
//    p.setPen(Qt:: white);

    p.setFont(font);
    p.drawText(30, 60, QString::number(score));

    for(int i = 0; i < 10; i++)
    {
        Letter *l = listLetter[i];
        QPixmap pixmap = QPixmap("://res/images/apple_red.png").scaledToHeight(50);
        p.setPen(l->getColor());
        p.drawPixmap(l->getPosX(), l->getPosY(), pixmap);//形参需要引用，则直接创建对应对象的变量
        p.drawText(l->getPosX() + pixmap.size().width() / 2 - 10, l->getPosY() + pixmap.size().height() / 2 + 15, QString(l->getValue()));
    }
}

void word_game:: mousePressEvent(QMouseEvent *e)
{
    for(int i = 0; i < 10; i++)
    {
        Letter *l = listLetter[i];
        QRect r(QPoint(l->getPosX(), l->getPosY()), l->getSize());

        if(r.contains(e->pos()))
        {
            move_w = e->pos().rx() - l->getPosX();
            move_h = e->pos().ry() - l->getPosY();
            isHit = true;
            //qDebug() << i;
            focusAppleID = i;
            //qDebug() << isHit;
            //qDebug() << listLetter[focusAppleID] -> getPosX() << listLetter[focusAppleID] -> getPosY();
            break;
        }
        else
        {
            isHit = false;
        }

        qDebug() << isHit;
    }
}

void word_game:: mouseMoveEvent(QMouseEvent *e)
{
   qDebug() << "mouseMoveEvent: " << e->pos();

   if(isHit == true)
   {
       if(e->pos().rx() <= width() || e->pos().ry() <= height())
       {
           listLetter[focusAppleID]->setPos(e->pos().rx()- move_w, e->pos().ry() - move_h);
       }
   }

   update();

}

void word_game:: mouseReleaseEvent(QMouseEvent *e)
{
    listLetter[focusAppleID]->setPos(e->pos().rx() - move_w, e->pos().ry() - move_w);
    isHit = false;
}
